# -*- coding: utf8 -*-
# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages

import wx                        # This module uses the new wx namespace
import sys
import os
import codecs

# import images                  # For PyEmbeddedImage

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

path = sys.path[0].decode(sys.getfilesystemencoding())
appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]

#---------------------------------------------------------------------------

class My_Memento(wx.Dialog):
     """ Crée la classe du Memento. """

     def on_set_focus(self, event):
         event.Skip()
     def on_kill_focus(self, event):
         event.Skip()
     def on_window_destroy(self, event):
         event.Skip()

     def __init__(self, parent):
         wx.Dialog.__init__(self, parent, -1,
                            title=_("Memento"),
                            pos=(-1, -1), size=(352, 312),
                            style=wx.DEFAULT_FRAME_STYLE ^ (wx.MINIMIZE_BOX))
         
         # Bind the close event to an events handler
         self.Bind(wx.EVT_CLOSE, self.on_close_window)
         
         #------------------------------------------------------------------
        
         self.parent = parent
         
         #------------------------------------------------------------------
         
         # self.SetMaxSize((700, 600))
         self.SetMinSize((352, 312))        

         #------------------------------------------------------------------
        
         # Simplified init method
         self.create_widget()
         self.do_layout()

         #------------------------------------------------------------------  
         
         self.Centre(wx.BOTH)
         print ""
         print "Display the memento dialog"
         
         #------------------------------------------------------------------
         
         self.btnClose = self.ShowModal()
        
     #----------------------------------------------------------------------

     def create_widget(self):
         bitmapDir = os.path.join(appDir, "bitmaps")
         bmp = wx.Bitmap(os.path.join(bitmapDir, "logo_Notes.png"))
         # or PyEmbeddedImage
         # bmp = images.Logo_Notes.GetBitmap()
         self.logo = wx.StaticBitmap(self, -1)
         self.logo.SetBitmap(bmp)
         
         #-------------
         
         self.label = wx.StaticText(self, -1, _("Little reminder..."))
         self.label.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
         
         #-------------
        
         self.field = wx.TextCtrl(self, -1, value="", size=(100, 165),
                                  style=wx.TE_LEFT | wx.TE_MULTILINE |
                                  wx.BORDER_THEME)

         # wx.Font(pointSize, family, style, weight, underline, faceName)
         fontSize = self.GetFont().GetPointSize()
         
         if wx.Platform == "__WXGTK__":
             self.field.SetFont(wx.Font(fontSize-1, wx.SWISS, wx.NORMAL, wx.NORMAL))

         elif wx.Platform == "__WXMAC__":
             self.field.SetFont(wx.Font(fontSize, wx.SWISS, wx.NORMAL, wx.NORMAL))       

         else:
             self.field.SetFont(wx.Font(fontSize, wx.SWISS, wx.NORMAL, wx.NORMAL))

         filename = codecs.open(os.path.join(path, u'memento'), 'r', 'utf-8')
         text_in = filename.read()
         filename.close()
         self.field.SetValue(text_in)

         # Method without codecs module          
         # mementoDir = os.path.join(appDir, "memento")
         # filename = open(os.path.join(mementoDir, "Memento.txt"), "r")
         # text_in = filename.read()
         # filename.close()
         # text_out = text_in.decode("utf-8", "ignore")
         # self.field.SetValue(text_out)
         
         self.field.SetInsertionPoint(0)
         
         # Bind the focus event to an events handler
         self.Bind(wx.EVT_TEXT, self.evt_text, self.field)
         self.field.Bind(wx.EVT_CHAR, self.evt_char)
         self.field.Bind(wx.EVT_SET_FOCUS, self.on_set_focus)
         self.field.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus)
         self.field.Bind(wx.EVT_WINDOW_DESTROY, self.on_window_destroy)
         self.field.Bind(wx.EVT_SET_FOCUS, self.on_clear_selection)
         
         #-------------
         
         self.btnSave = wx.Button(self, id=wx.ID_SAVE, label="")

         self.btnClose = wx.Button(self, id=wx.ID_CLOSE, label="")
         
         # Bind the button event to an events handler
         self.Bind(wx.EVT_BUTTON, self.on_save, self.btnSave)
         self.Bind(wx.EVT_BUTTON, self.on_close, self.btnClose)
         
         self.btnSave.SetDefault()
         self.btnClose.SetFocus()


     def do_layout(self):
         txtLogoSizer = wx.BoxSizer(wx.HORIZONTAL)
         fieldSizer = wx.BoxSizer(wx.HORIZONTAL)
         btnSizer = wx.BoxSizer(wx.HORIZONTAL)
         mainSizer = wx.BoxSizer(wx.VERTICAL)
         
         txtLogoSizer.Add(self.logo, 0, wx.ALL, 10)
         txtLogoSizer.Add(self.label, 0, wx.TOP | wx.RIGHT, 25)
         fieldSizer.Add(self.field, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 10)
         btnSizer.Add(self.btnSave, 0, wx.ALL, 10)
         btnSizer.Add(self.btnClose, 0, wx.TOP | wx.BOTTOM | wx.RIGHT, 10)
         mainSizer.Add(txtLogoSizer, 0, wx.ALIGN_LEFT, 0)
         mainSizer.Add(fieldSizer, 1, wx.EXPAND, 0)
         mainSizer.Add(btnSizer, 0, wx.ALIGN_RIGHT)

         self.SetAutoLayout(True)
         self.SetSizer(mainSizer)
         mainSizer.Fit(self)

          
     def evt_text(self, event):
         pass


     def evt_char(self, event):
         event.Skip()

        
     def on_clear_selection(self, event):
         ip = self.field.GetInsertionPoint()
         lp = self.field.GetLastPosition()
   

     def on_save(self, event):
         text_out = self.field.GetValue()            
         filename = codecs.open(os.path.join(path, u'Memento'), mode='w', encoding='utf-8')
         filename.write(text_out)
         filename.close()

         # Method without codecs module         
         # text_out = self.field.GetValue()            
         # text_out = text_out.encode("utf-8", "ignore")
         # mementoDir = os.path.join(appDir, "memento")
         # filename = open(os.path.join(mementoDir, "Memento.txt"), "w")
         # filename.write(text_out)
         # filename.close()
         

     def on_close(self, event):
         self.Close(True)
     
     
     def on_close_window(self, event):
         self.Destroy()


